<template>
  <div class="sub-category">
    <div class="container">
      <!-- 面包屑 -->
      <SubBread />
      <!-- 筛选区 
      1：组件html
      2：调用接口渲染数据
      3：完成点击逻辑处理
      4：return接口调用所需要参数
        a:需要知道商品列表接口调用需要什么参数
        b:根据现有数据给出相应的参数值
      
      -->
      <!-- 筛选区  -->
      <SubFilter @change="fliterChange"></SubFilter>
      <!-- 复选框 
     子组件标签 v-model:属性名=“值”  属性名：modelValue才可以省略
     子组件内
        props:['属性名'],
        emit('update:属性名',值) 
      -->
      <!-- <XtxCheckbox v-model="checkValue" @change="changeEvent"
        >复选框文本内容区域</XtxCheckbox
      > -->
      <div class="goods-list">
        <!-- 排序 -->
        <SubSort @change="sortChange"></SubSort>
        <!-- 列表 
        1:基本布局（html+css）
        2:定义api调用api获取数据渲染
        3:上拉加载组件处理
        -->
        <ul>
          <li v-for="i in list" :key="i.id">
            <GoodsItem :goods="i" />
          </li>
        </ul>
        <!-- 上拉加载组件 -->
        <PullLoad
          :loading="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="load"
        ></PullLoad>
      </div>
    </div>
  </div>
</template>

<script>
import SubBread from './components/sub-bread'
import SubFilter from './components/sub-filter.vue'
import { onBeforeMount, onMounted, ref, watch } from 'vue'
import SubSort from './components/sub-sort.vue'
import GoodsItem from './components/goods-item.vue'
import { categoryGoods } from '@/api/category'
import PullLoad from './components/PullLoad.vue'
export default {
  name: 'SubCategory',
  components: { SubBread, SubFilter, SubSort, GoodsItem, PullLoad },
  props: ['id'],
  setup (props) {
    // console.log('sub:setup')
    // onBeforeMount(() => {
    //   console.log('sub:onBeforeMount')
    // })
    // onMounted(() => {
    //   console.log('sub:onMounted')
    // })
    let paramsApi = {
      categoryId: props.id,
      page: 1,
      pageSize: 5
    }

    // 属性筛选
    function fliterChange (params) {
      // console.log(params)
      paramsApi = { ...paramsApi, ...params, page: 1 }
      list.value = []
      finished.value = false
      load()
    }
    // 排序筛选
    function sortChange (params) {
      // console.log(params)
      params = { ...params, sortMethod: params.sortMethod || 'desc' }
      paramsApi = { ...paramsApi, ...params, page: 1 }
      list.value = []
      finished.value = false
      load()
    }
    // const checkValue = ref(false)
    // function changeEvent (val) {
    //   console.log(val)
    // }

    // 进入页面获取商品列表

    const list = ref([])
    const loading = ref(false)
    const finished = ref(false)
    function load () {
      loading.value = true
      categoryGoods(paramsApi).then(res => {
        loading.value = false
        list.value.push(...res.result.items)
        // 判断是不是所有数据已加载完成
        if (list.value.length >= res.result.counts) {
          finished.value = true
        }
        paramsApi.page++
        // console.log('列表数据111', res)
      })
    }
    // 每一次切换类目，所有数据还原
    watch(
      () => props.id,
      () => {
        paramsApi = {
          categoryId: props.id,
          page: 1,
          pageSize: 5
        }
        list.value = []
        finished.value = false
      }
    )

    return { fliterChange, sortChange, list, loading, finished, load }
  }
}
</script>
<style lang="less" scoped>
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li {
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>
